$(function () {

    let agent = getQueryString("agent");

    let url = '../withdrawbill/list?1=1';
    if (agent) {
        url += '&agent=' + agent;
    }

    $("#jqGrid").jqGrid({
        url: url,
        datatype: "json",
        colModel: [
            {label: 'id', align: "center", name: 'id', index: 'id', key: true, hidden: true},
            {
                label: '会员昵称', align: "center", name: 'nickName', index: 'nickName', width: 80
                , formatter: nLink,
                editable: true
            },
            {
                label: '会员手机号', align: "center", name: 'userMobile', index: 'userMobile', width: 80
                , formatter: pLink,
                editable: true
            }
            ,
            {
                label: '账号名称', align: "center", name: 'alipayName', index: 'alipay_name', width: 80
            }
            ,
            {
                label: '提现账号', align: "center", name: 'alipayNo', index: 'alipay_no', width: 80
            }
            ,
            {
                label: '创建时间', align: "center", name: 'createDate', index: 'create_date', width: 80,
                formatter: function (value) {
                    return transDate(value);
                }
            }
            ,
            {
                label: '提现金额', align: "center", name: 'amount', index: 'amount', width: 80
            }
            ,
            {
                label: '状态', align: "center", name: 'status', index: 'status', width: 80,
                formatter: sLink,
                editable: true
            }
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader:
            {
                root: "page.list",
                page:
                    "page.currPage",
                total:
                    "page.totalPage",
                records:
                    "page.totalCount"
            }
        ,
        prmNames: {
            page: "page",
            rows:
                "limit",
            order:
                "order"
        }
        ,
        gridComplete: function () {
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    })
    ;
});


function nLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.nickName + '</a>';
}

function pLink(cellvalue, options, rowObject) {
    return '<a href="javascript:void(0)" onclick="getinfo(' + rowObject.id + ')">' + rowObject.userMobile + '</a>';
}

function sLink(cellvalue, options, rowObject) {
    if (rowObject.status == 0) {
        return '<a href="javascript:void(0)" onclick="auding(' + rowObject.id + ')"><span class="label label-info">审核中</span></a>';
    }
    if (rowObject.status == 1) {
        return '<span class="label label-danger">提现失败</span>';
    }
    if (rowObject.status == 2) {
        return '<span class="label label-success">提现成功</span>';
    }
}

function getinfo(id) {
    vm.showList = false;
    vm.title = "提现详情";

    vm.getInfo(id);
}


function auding(id) {
    openWindow({
        title: "快速审核",
        area: ['600px', '250px'],
        content: jQuery("#staticAuding"),
        btn: ['确定', '取消'],
        btn1: function (index) {
            vm.apiWithdrawBill.id = id;
            layer.close(index);
            $.ajax({
                type: "POST",
                url: "../withdrawbill/update",
                contentType: "application/json",
                data: JSON.stringify(vm.apiWithdrawBill),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);

                    }

                }
            });


        }
    });
}

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        apiWithdrawBill: {},
        ruleValidate: {
            remark: [
                {required: true, message: '审核信息必填', trigger: 'blur'}
            ]
        },
        q: {
            name: '',
            phone: '',
            nickName: ''
        }
    },
    methods: {
        query: function () {
            vm.reload();
        },

        update: function (event) {
            let id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "提现详情";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            console.log(vm.apiWithdrawBill);

            var status = vm.apiWithdrawBill.status;

            if (status == 0) {
                alert('审核状态必填');
                return;
            }


            let url = vm.apiWithdrawBill.id == null ? "../withdrawbill/save" : "../withdrawbill/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.apiWithdrawBill),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },

        getInfo: function (id) {
            $.get("../withdrawbill/info/" + id, function (r) {
                vm.apiWithdrawBill = r.apiWithdrawBill;

            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'alipayName': vm.q.alipayName, 'phone': vm.q.phone, 'nickName': vm.q.nickName},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        }
    }
});